import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'marketAnalysisM',
  state: {},
  effects: {
    // 获取因子收益率数据风格因子
    *getStyleFactorYield({ payload, callback }, { call, put }) {
      const res = yield call(api.marketAnalysis.getStyleFactorYield, payload);
      if (res && res.code == 0) {
        res.data.series.length &&
          res.data.series.map((item) => {
            item.data =
              item.data.length &&
              item.data.map((e) => {
                return (Number(e) * 100).toFixed(2);
              });
          });
        callback(res.data);
      } else {
        message.error(res?.msg || '获取失败！');
      }
    },
    // 获取因子收益率数据行业因子
    *getIndustryFactorYield({ payload, callback }, { call, put }) {
      const res = yield call(api.marketAnalysis.getIndustryFactorYield, payload);
      if (res && res.code == 0) {
        res.data.series.length &&
          res.data.series.map((item) => {
            item.data =
              item.data.length &&
              item.data.map((e) => {
                return (Number(e) * 100).toFixed(2);
              });
          });
        callback(res.data);
      } else {
        message.error(res?.msg || '获取失败！');
      }
    },
    // 获取因子风险风格因子
    *getStyleFactorRisk({ payload, callback }, { call, put }) {
      const res = yield call(api.marketAnalysis.getStyleFactorRisk, payload);
      if (res && res.code == 0) {
        res.data.series.length &&
          res.data.series.map((item) => {
            item.data =
              item.data.length &&
              item.data.map((e) => {
                return (Number(e) * 100).toFixed(2);
              });
          });
        callback(res.data);
      } else {
        message.error(res?.msg || '获取失败！');
      }
    },
    // 获取因子风险行业因子
    *getIndustryFactorRisk({ payload, callback }, { call, put }) {
      const res = yield call(api.marketAnalysis.getIndustryFactorRisk, payload);
      if (res && res.code == 0) {
        res.data.series.length &&
          res.data.series.map((item) => {
            item.data =
              item.data.length &&
              item.data.map((e) => {
                return (Number(e) * 100).toFixed(2);
              });
          });
        callback(res.data);
      } else {
        message.error(res?.msg || '获取失败！');
      }
    },
    // 获取最高收益率
    *getMaxYield({ payload, callback }, { call, put }) {
      const res = yield call(api.marketAnalysis.getMaxYield, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res?.msg || '获取失败！');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
